package com.app.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.app.mapper.ProblemReflectionMapper;
import com.app.model.PatrolGroup;
import com.app.model.ProblemReflection;
import com.app.model.SysUser;
import com.app.page.CommonPage;
import com.app.request.PageParamRequest;
import com.app.request.ProblemRequest;
import com.app.response.ArticleResponse;
import com.app.service.IPatrolGroupService;
import com.app.service.IProblemReflectionService;
import com.app.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.yulichang.query.MPJQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 问题反映 服务实现类
 * </p>
 *
 * @author ghoul
 * @since 2024-12-17
 */
@Service
public class ProblemReflectionServiceImpl extends ServiceImpl<ProblemReflectionMapper, ProblemReflection> implements IProblemReflectionService {
    @Autowired
    private IPatrolGroupService patrolGroupService;
    @Autowired
    private ISysUserService sysUserService;

    @Override
    public PageInfo<ProblemReflection> getAdminList(ProblemRequest request, PageParamRequest pageParamRequest) {
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        SysUser sysUser = sysUserService.getById(loginIdAsInt);
        Page<ProblemReflection> problemReflectionPage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        MPJQueryWrapper<ProblemReflection> queryWrapper = new MPJQueryWrapper<>();
        queryWrapper.selectAll(ProblemReflection.class);
        queryWrapper.select("pg.name as departmentText");
        queryWrapper.leftJoin("lm_patrol_group pg on t.department_id = pg.id");
        queryWrapper.eq("t.is_del", 0);
        if (sysUser.getRoleId() == 2) {
            queryWrapper.eq("t.inspection_team", sysUser.getPatrolGroupId());
        }
        if (StrUtil.isNotBlank(request.getKeyword())) {
            queryWrapper.like("t.name", request.getKeyword()).or().like("t.phone", request.getKeyword());
        }
        if (request.getIsAnonymous() != null) {
            queryWrapper.eq("t.is_anonymous", request.getIsAnonymous());
        }
        List<ProblemReflection> list = list(queryWrapper);
        return CommonPage.copyPageInfo(problemReflectionPage, list);
    }

    @Override
    public boolean create(ProblemReflection request) {
        PatrolGroup patrolGroup = patrolGroupService.getById(request.getDepartmentId());
        if (patrolGroup == null) {
            return false;
        }
        request.setInspectionTeam(patrolGroup.getPid());
        request.setCreateTime(LocalDateTime.now());
        return save(request);
    }

    @Override
    public boolean deleteById(Integer id) {
        ProblemReflection reflection = getById(id);
        if (reflection == null) {
            return false;
        }
        reflection.setIsDel(true);
        reflection.setUpdateTime(LocalDateTime.now());
        return updateById(reflection);
    }

    @Override
    public ArticleResponse getDetail(Integer id) {
        return null;
    }
}
